package com.fastjrun.util;

import com.aliyun.alidns20150109.models.*;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;

@Data
public class AliYunUtil {
    com.aliyun.alidns20150109.Client client = null;

    private String regionId = "cn-hangzhou";
    private String accessKeyId = "";
    private String accessKeySecret = "";

    public void init() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "alidns.cn-hangzhou.aliyuncs.com";
        client = new com.aliyun.alidns20150109.Client(config);
    }

    public String addDomainRecord(String domainName, String ip, String rR, String type) {
        AddDomainRecordRequest request = new AddDomainRecordRequest();
        request.setDomainName(domainName);
        request.setValue(ip);
        request.setRR(rR);
        request.setTTL(600L);
        request.setLine("default");
        request.setType(type);
        String recordId = "";
        try {
            AddDomainRecordResponse response = client.addDomainRecord(request);
            recordId = response.getBody().getRecordId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return recordId;
    }

    public boolean updateATypeDomainRecord(String recordId, String ip, String rR) {
        return this.updateDomainRecord(recordId, ip, rR, "A");
    }

    public boolean updateDomainRecord(String recordId, String ip, String rR, String type) {
        UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
        request.setRecordId(recordId);
        request.setValue(ip);
        request.setRR(rR);
        request.setTTL(600L);
        request.setLine("default");
        request.setType(type);
        try {
            UpdateDomainRecordResponse response = client.updateDomainRecord(request);
            String recordIdRes = response.getBody().getRecordId();
            if (recordId.equals(recordIdRes)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String queryATypeDomainRecordId(String domainName, String rR) {
        DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest();
        describeDomainRecordsRequest.setDomainName(domainName);
        describeDomainRecordsRequest.setRRKeyWord(rR);
        String recordId = "";
        try {
            DescribeDomainRecordsResponse describeDomainRecordsResponse =
                    client.describeDomainRecords(describeDomainRecordsRequest);
            if (describeDomainRecordsResponse.getBody().getDomainRecords() != null
                    && describeDomainRecordsResponse.getBody().getDomainRecords().record.size() > 0) {
                recordId = describeDomainRecordsResponse.getBody().getDomainRecords().record.get(0).getRecordId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return recordId;
    }
}
